package allwe.nacos.controller;

import allwe.nacos.service.DemoService;
import allwe.nacos.service.FeignProviderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Value("${service-url.nacos.user-service}")
    private String nacosProvider;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private FeignProviderService providerService;

    @DubboReference
    private DemoService demoService;

    @GetMapping("/hello")
    public String hello() {

        String url = nacosProvider + "/hello";
        Class<String> responseBodyClass = String.class;
        String requestBody = "";

        return restTemplate.getForObject(url, responseBodyClass, requestBody);
    }

    @GetMapping("/openFeign")
    public String openFeign() {
        System.out.println("openFeign");
        return providerService.hello();
    }

    @GetMapping("/sayHello")
    public String sayHello() {
        System.out.println("sayHello");
        return demoService.sayHello("dubbo");
    }

    @GetMapping("/sayHello1")
    public String sayHello1() {
        return "sayHello";
    }
}
